# -*- coding: utf-8 -*-
import os
def image_copy():
    with open(r'F:\pythonWorkSpace\study\data\images\image1.png', 'rb') as f:
        img = f.read()
        # print(img)
    with open(r'F:\pythonWorkSpace\study\data\images\image2.png', 'wb') as f1:
        f1.write(img)

def rename_file():
    os.rename(r'F:\pythonWorkSpace\study\data\images\image2.png', r'F:\pythonWorkSpace\study\data\images\image3.png')
    print(os.listdir())
    os.remove(r'F:\pythonWorkSpace\study\data\images\image3.png')
    #创建目录
    os.mkdir(r'F:\pythonWorkSpace\study\data\test1')
    os.mkdir(r'F:\pythonWorkSpace\study\data\test2')
    #删除目录
    os.rmdir(r'F:\pythonWorkSpace\study\data\test2')

if __name__ == '__main__':
    print('你好，世界！！！')
    f = open(r'F:\pythonWorkSpace\study\data\test.txt')
    print(f.name)
    lines = f.readlines()
    print(lines)
    for line in lines:
        print(line.strip())
    f.close()
    f = open(r'F:\pythonWorkSpace\study\data\test.txt')
    while True:
        line = f.readline()
        # 没有读到数据，退出循环
        if not line:
            break
        else:
            print(line.strip())
    f.close()
    #访问模式w:只写，r：只读，w+：先写再读，存在文件就重新编辑，不存在则创建，r+:可读写文件，+影响读写效率，a：追加，不存在则创建，存在则在原有基础上添加新的内容
    f = open(r'F:\pythonWorkSpace\study\data\test1.txt','w+')
    f.write('123')
    f.write('ss1s')
    f.close()
    f = open(r'F:\pythonWorkSpace\study\data\test1.txt', 'a')
    f.write('\n123')
    f.write('ss1s')
    f.close()
    #文件定位操作,tell()：显示文件内容当前指针位置、seek(offset,whence)：移动文件指针到指定的位置,offset表示移动的字节数，whence表示起始位置，0表示从文件开头起，1表示从当前位置起，2表示从文件末尾起
    f = open(r'F:\pythonWorkSpace\study\data\test1.txt', 'wb+')
    f.write(b'123')
    index = f.tell()
    print(index)
    index = f.seek(-1,1)
    print(f.read())
    f.close()
    #with open：代码执行完会自动调用f.close()方法关闭文件，windows默认使用gbk编码
    with open(r'F:\pythonWorkSpace\study\data\test2.txt', 'w+',encoding='utf-8') as f:
        f.write('哈哈哈')
    print(f.closed)
    image_copy()
    rename_file()

